package com.adou.d10.reflect;

import org.junit.Test;

import java.lang.reflect.Field;

public class TestFiled {
    @Test
    public void testGetFileds() throws Exception {
        // 1.活动对象
        Class c = Cat.class;

        // 2.获取类中成员变量
//        Field[] fields = c.getFields();
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName() + " " + field.getType());
        }

        // 定位某个成员变量
        Field fName = c.getDeclaredField("name");
        System.out.println(fName.getName());


        Field fAge = c.getDeclaredField("age");
        System.out.println(fAge.getName());

        // 赋值
        Cat cat = new Cat();
        fName.setAccessible(true);
        fName.set(cat, "咖啡猫");
        System.out.println(cat);

        // 取值
        String name = (String)fName.get(cat);
        System.out.println(name);


    }
}
